package com.example.mykotilin.workmanager

import android.annotation.SuppressLint
import android.app.Application
import android.provider.SyncStateContract
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.work.WorkManager
import com.example.mykotilin.workmanager.lib.Constants
import com.example.mykotilin.workmanager.lib.ImageOperations

class FilterViewModel(application: Application) : ViewModel() {
    private val workManager = WorkManager.getInstance(application)

    internal val workInfo = workManager.getWorkInfosByTagLiveData(Constants.TAG_OUTPUT)

    internal fun apply(imageOperations: ImageOperations) {
        imageOperations.continuation.enqueue()
    }

    internal fun cancel() {
        workManager.cancelUniqueWork(Constants.IMAGE_MANIPULATION_WORK_NAME)
    }

}

class FilterViewModelFactory(private val application: Application) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return if (modelClass.isAssignableFrom(FilterViewModel::class.java)) {
            FilterViewModel(application) as T
        } else {
            throw IllegalArgumentException("Unknown ViewModel class")
        }
    }

}